package freedom.sample.ws.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

import freedom.sample.ws.model.Author;
import freedom.sample.ws.model.Book;
import freedom.sample.ws.service.BookService;

public class BookServiceImpl implements BookService {
	public Log logger = LogFactory.getLog(getClass());

	@Override
	public String getBookById(int id) throws JsonProcessingException {
		Book book = new Book();
		book.setBookName("book_" + id);
		Author author = new Author();
		author.setAuthorName("author_" + id);
		book.setBookAuthor(author);
		logger.info("call getBookById. return: " + book);
		return genXml(book);
	}
	@Override
	public Book getBookById2(int id) {
		Book book = new Book();
		book.setBookName("book_" + id);
		Author author = new Author();
		author.setAuthorName("author_" + id);
		book.setBookAuthor(author);
		logger.info("call getBookById2. return: " + book);
		return book;
	}

	@Override
	public String sayHello(String str) {
		String msg = "Hello, " + str;
		logger.info("call sayHello. return: " + msg);
		return msg;
	}

    public String genXml(Object object) throws JsonProcessingException
    {
        XmlMapper xmlMapper = new XmlMapper();
        String xmlStr = xmlMapper.writeValueAsString(object);
        return xmlStr;
    }
}
